<template>
  <div style="padding: 20px">
    <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="样品剂型及考察项目" name="first">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="100px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name"
                  placeholder="请输入样品剂型名称"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="small"
                  @click="handleQuery"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType"
                  >新增</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div>
          <el-table :data="data" size="medium" border>
              <el-table-column
              label="序号"
              align="center"
              prop="index"
              width="80"
            >
            <template slot-scope="scope">
            <span>{{
              (pages.pageNum - 1) * pages.pageSize + scope.$index + 1
            }}</span>
          </template>
            </el-table-column>
            <el-table-column
              label="样品剂型名称"
              align="center"
              prop="sample_type_name"
            ></el-table-column>
            <el-table-column
              label="考察项目"
              align="center"
              prop="relation_list_str"
            >
            </el-table-column>
            <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
              <template slot-scope="scope">
                <el-button type="text"  size="meduim"
                @click="editType(scope.row)"
                  >编辑
                </el-button>
                <el-button
                  class="text-btn-red"
                  type="text"
                  size="meduim"
                  @click="deleteItem(scope.row, scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total"
          :page.sync="pages.pageNum" :limit.sync="pages.pageSize" @pagination="afreshList" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="考察项目" name="second">
        <div>
          <el-form
            style="width: 100%; display: flex; justify-content: space-between"
            ref="queryForm"
            :model="queryForm"
            :inline="true"
            label-width="100px"
          >
            <div>
              <el-form-item label="">
                <el-input
                  v-model="queryForm.name1"
                  placeholder="请输入考察项目名称"
                  style="width: 400px"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="small"
                  @click="handleQuery1"
                  >查询
                </el-button>
              </el-form-item>
            </div>
            <div style="float: right">
              <el-form-item>
                <el-button type="primary" icon="el-icon-plus" @click="addType1"
                  >添加项目</el-button
                >
              </el-form-item>
            </div>
          </el-form>
        </div>
        <el-table :data="data1" size="medium" border>
            <el-table-column
              label="序号"
              align="center"
              prop="index"
              width="80"
            >
            <template slot-scope="scope">
            <span>{{
              (pages1.pageNum - 1) * pages1.pageSize + scope.$index + 1
            }}</span>
          </template>
            </el-table-column>
          <el-table-column
            prop="inspect_project_name"
            label="考察项目名称"
            align="center"
          >
          </el-table-column>
          <el-table-column label="操作" 
            align="left"
            header-align="center"
            width="180">
            <template slot-scope="scope">
              <el-button type="text"  size="meduim"
                @click="editType1(scope.row)"
                  >编辑
                </el-button>
              <el-button
                class="text-btn-red"
                type="text"
                size="meduim"
                @click="deleteItem1(scope.row, scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination style="display:flex;justify-content:flex-end;padding-bottom:0px!important" :total="total1"
          :page.sync="pages1.pageNum" :limit.sync="pages1.pageSize" @pagination="afreshList1" />
      </el-tab-pane>
    </el-tabs>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="800px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="样品剂型" prop="name">
          <el-input v-model="form.name" placeholder="请输入样品剂型名称"></el-input>
        </el-form-item>
        <el-form-item label="考察项目" prop="project">
           <el-transfer v-model="value" :data="datas" :titles="titles" @change="changeProject()"></el-transfer>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose">取消</el-button>
        <el-button plain @click="cSubmitForm" v-if="this.title =='添加样品剂型'">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-dialog>   
    <el-dialog
      :title="title1"
      :visible.sync="open1"
      width="600px"
      append-to-body
      :destroy-on-close="true"
      :before-close="handleClose"
    >
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="110px">
        <el-form-item label="考察项目名称" prop='name1'>
           <el-input v-model="form1.name1" placeholder="请输入考察项目名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="handleClose">取消</el-button>
        <el-button plain @click="cSubmitForm1" v-if="this.title1 =='添加考察项目'">保存并继续添加</el-button>
        <el-button type="primary" @click="submitForm1">确定</el-button>
      </div>
    </el-dialog>      
  </div>
</template>

<script>
import {sampleTypeList,projectList,projectSList,sampleTypeCreate, sampleTypeUpdate,sampleTypeDelete,projectCreate,projectUpdate,projectDelete} from "@/api/stability";
export default {
  data() {
     let name = (rule, value, callback) => {
      if (this.form.name != '') {
        if(this.form.name.split(" ").join("").length == 0){
          callback(new Error("样品剂型名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("样品剂型名称不能为空"));
      }
    };
     let name1 = (rule, value, callback) => {
      if (this.form1.name1 != '') {
        if(this.form1.name1.split(" ").join("").length == 0){
          callback(new Error("考察项目名称不能全是空格"));
        }else{
          callback();
        }
      } else {
        callback(new Error("考察项目名称不能为空"));
      }
    };
    let project = (rule, value, callback) => {
      if (this.value.length != 0) {
        callback();
      } else {
        callback(new Error("请选择考察项目"));
      }
    };
    return {
       title:'添加样品剂型',
       title1:'添加考察项目',
       pages:{
        pageNum:1,
        pageSize:10

      },
      pages1:{
        pageNum:1,
        pageSize:10

      },
      total:0,
      total1:0,
      titles:['可选项目','已选考察项目'],
      datas: [],
      value: [],
      activeName: "first",
      queryForm: {
        type: "",
        name: "",
        name1: "",
      },
      current: null,
      isEdit: false,
      values: "",
      current: null,
      data: [
       
      ],
      data1: [
       
      ],
      open: false,
      open1: false,
      open2: false,
      form: {
        type: "",
        name:'',
        name1:'',
        project:[]
      },
      form1: {
        name1:'',
      },
      rules1: {
        name1: [
          { required: true, validator: name1, trigger: "blur" },
        ],
      },
      rules: {
        name: [
          { required: true, validator:name, trigger: "blur" },
        ],
        // name1: [
        //   { required: true, validator: name1, trigger: "blur" },
        // ],
        project:[
          { required: true, validator:project},
        ]
      },
      sList:[],
      relation_list:[],
      id:'',
      id1:''
    };
  },
  created() {
    this.getSampleTypeList()
    this.getProjectList()
    this.getSList()
  },
  methods: {
    getSList(){
       this.sList = []
       this.datas = []
       projectSList().then(res=>{
         if(res.code==200){
           this.sList = res.data
            const arr = []
            res.data.forEach((item,index)=>{
              let obj = {
                id:item.id,
                inspect_project_name:item.inspect_project_name
              }
              arr.push(obj)
            })
            arr.forEach((itm, index) => {
              this.datas.push({
                // id:itm.id,
                label: itm.inspect_project_name,
                key: itm.id,
              });
            });
         }
       })
    },
    changeProject(){
      console.log(this.form.project)
      this.relation_list=[]
      this.value.forEach(item=>{
        this.sList.forEach(itm=>{
          if(item == itm.id){
            let obj = {
              inspect_project_id:itm.id,
              inspect_project_name:itm.inspect_project_name
            }
            this.relation_list.push(obj)
          }
        })
      })
    },
    //获取样品类型列表
    getSampleTypeList(){
       let data ={
        condition:{
          sample_type_name:this.queryForm.name
        },
        pager:{
          limit:this.pages.pageSize,
          skip:this.pages.pageNum
        }
      }
      sampleTypeList(data).then(res=>{
        if(res.code==200){
          console.log(res.data)
          this.data = res.data.data
          this.total = res.data.total
        }
      })
    },
    //获取考察项目列表
    getProjectList(){
      let data ={
        condition:{
          inspect_project_name:this.queryForm.name1
        },
        pager:{
          limit:this.pages1.pageSize,
          skip:this.pages1.pageNum
        }
      }
      projectList(data).then(res=>{
        if(res.code==200){
          console.log(res.data)
          this.data1 = res.data.data
          this.total1 = res.data.total
        }
      })
    },
    handleQuery(){
       this.pages={
        pageNum:1,
        pageSize:10
      },
      this.getSampleTypeList()
    },
    handleQuery1(){
       this.pages1={
        pageNum:1,
        pageSize:10
      },
      this.getProjectList()
    },
    afreshList1(){
      this.getProjectList()
    },
    afreshList(){
      this.getSampleTypeList()
    },
    addType() {
        this.open = true
        this.title="添加样品剂型"
    },
    editType(row){
        this.open = true
        this.title="编辑样品剂型"
        console.log(row)
        this.form.name = row.sample_type_name
        this.id = row.id
        this.value=[]
        row.relation_list.forEach(item=>{
          this.value.push(item.inspect_project_id)
        })
        this.relation_list = row.relation_list
    },
    deleteItem(row,index){
       this.$confirm("是否确认删除 " + row.sample_type_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            sampleTypeDelete(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getSampleTypeList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    addType1() {
        this.open1 = true
        this.title1="添加考察项目"
    },
    editType1(row){
        this.open1 = true
        this.title1="编辑考察项目"
        this.form1.name1 = row.inspect_project_name
        this.id1 = row.id
    },
    deleteItem1(row,index){
       this.$confirm("是否确认删除 " + row.inspect_project_name	 + " ?", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then(() => {
            projectDelete(row.id).then(res => {
              if(res.code==200){
                this.$message.success('删除成功')
                this.getSampleTypeList()
                this.getProjectList()
                this.getSList()
              }else{
                this.$message.error(res.msg)
              }
            });
        })
        .catch(function() {});
    },
    handleClose() {
        this.open = false
        this.open1 = false
        this.form={
          type: "",
          name:'',
          name1:'',
        }
        this.value = []
    },
    handleClose1() {
        this.open1 = false
        this.form1={
          name1:'',
        }
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
            if(this.title =='添加样品剂型'){
              let data = {
              sample_type_name:this.form.name,
              relation_list:this.relation_list
            }
            sampleTypeCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSampleTypeList()
                this.handleClose()
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('project')
                  this.$refs.form.clearValidate('name')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }else{
            let data = {
              sample_type_name:this.form.name,
              relation_list:this.relation_list
            }
            sampleTypeUpdate(this.id,data).then(res=>{
              if(res.code==200){
                this.$message.success('编辑成功')
                this.getSampleTypeList()
                this.handleClose()
                this.$nextTick(() => {
                this.$refs.form.clearValidate('project')
                this.$refs.form.clearValidate('name')
              })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    cSubmitForm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
            if(this.title =='添加样品剂型'){
              let data = {
              sample_type_name:this.form.name,
              relation_list:this.relation_list
            }
            sampleTypeCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.form.name=''
                this.form.project = []
                this.value =[]
                this.$nextTick(() => {
                  this.$refs.form.clearValidate('project')
                  this.$refs.form.clearValidate('name')
                })
                this.getSampleTypeList()
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    cSubmitForm1(){
      this.$refs.form1.validate((valid) => {
        if (valid) {
           let data = {
              inspect_project_name:this.form1.name1,
            }
            projectCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSampleTypeList()
                this.getProjectList()
                this.getSList()
                this.form1.name1=''
                this.$nextTick(() => {
                  this.$refs.form1.clearValidate('name1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
        }
      })
    },
    submitForm1() {
      this.$refs.form1.validate((valid) => {
        if (valid) {
            if(this.title1 =='添加考察项目'){
              let data = {
              inspect_project_name:this.form1.name1,
            }
            projectCreate(data).then(res=>{
              if(res.code==200){
                this.$message.success('添加成功')
                this.getSampleTypeList()
                this.getProjectList()
                this.getSList()
                this.open1 = false
                this.handleClose1()
                this.$nextTick(() => {
                  this.$refs.form1.clearValidate('name1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }else{
             let data = {
              inspect_project_name:this.form1.name1,
            }
            projectUpdate(this.id1,data).then(res=>{
              if(res.code==200){
                this.$message.success('编辑成功')
                this.getSampleTypeList()
                this.getProjectList()
                this.getSList()
                this.open1 = false
                this.$nextTick(() => {
                  this.$refs.form1.clearValidate('name1')
                })
              }else{
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    handleClick() {
      this.handleClose()
      this.handleClose1()
      // this.form={
      //   type: "",
      //   name:'',
      //   name1:'',
      // }
      // this.value=[]
    },
  },
};
</script>
<style lang="scss">
</style>